package com.xyrl.project.common.salesman.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyrl.common.base.exception.XyrlException;
import com.xyrl.common.core.properties.XyrlProperties;
import com.xyrl.project.common.authentication.JWTUtil;
import com.xyrl.project.common.authentication.LoginInfo;
import com.xyrl.project.common.constant.XyrlConstant;
import com.xyrl.project.common.salesman.service.SalesmanService;
import com.xyrl.project.common.salesman.vo.UserLoginVo;
import com.xyrl.project.common.system.dao.UserMapper;
import com.xyrl.project.common.system.entity.User;
import com.xyrl.project.common.system.service.LoginLogService;
import com.xyrl.project.common.system.vo.UserVo;
import com.xyrl.project.common.util.MD5Util;
import com.xyrl.project.common.util.XyrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 *   * @className SalesmanServiceImpl
 *   * @company 星纭融链科技服务有限公司
 *   * @author 徐恩源
 *   * @version 1.0
 *   * @date 2019-12-27 15:42:16
 *   * @Description 
 *  
 */
@Service("salesmanServiceImpl")
@Slf4j
public class SalesmanServiceImpl extends ServiceImpl<UserMapper, User> implements SalesmanService {
    @Autowired
    private LoginLogService loginLogService;
    @Autowired
    private XyrlProperties properties;

    @Override
    public User findByName(String username) {
        return baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getAccount, username).eq(User::getIsDelete, XyrlConstant.NO));
    }

    @Override
    @Transactional
    public UserVo loginBySales(UserLoginVo userLoginVo) throws XyrlException {
        User user = this.findByName(userLoginVo.getUsername());
        if (null == user || !User.USER_TYPE_SALESMAN.equals(user.getUserType())) {
            throw new XyrlException("输入的账号不存在");
        }
        if (User.STATUS_LOCK.equals(user.getStatus())) {
            throw new XyrlException("当前账号已被冻结!");
        }
        String pwd = MD5Util.encrypt(userLoginVo.getUsername(), userLoginVo.getPwd());
        if (!user.getPassword().equals(pwd)) {
            throw new XyrlException("账号或密码错误");
        }
        //添加用户缓存信息  用户登录token
        LoginInfo info = new LoginInfo(userLoginVo.getUsername(), XyrlConstant.USER_TYPE_SALESMAN);
        String generToken = JWTUtil.sign(info, user.getPassword());
        //取得token 值
        String token = XyrlUtil.encryptToken(generToken);
        log.info("取得用户token值:{},\r\t 加密后的token:{}", generToken, token);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        userVo.setToken(token);
        user.setLastLoginTime(new Date());
        //修改用户登录时间
        updateUserLoginTime(user.getUserId());
        return userVo;
    }
    private void updateUserLoginTime(Long userId){
        UpdateWrapper<User> update = new UpdateWrapper<>();
        update.set("LAST_LOGIN_TIME", new Date());
        update.eq("USER_ID", userId);
        baseMapper.update(new User(), update);
    }
}
